//
//  SickWriteController.m
//  QuickCure1
//
//  Created by JianFeng on 16/11/4.
//  Copyright © 2016年 maoge. All rights reserved.
//
#import "ChooseTableController.h"
#import "PresonMondel.h"
#import "LabelGroupView.h"
#import "SickWriteController.h"
#import "SickPushImageView.h"
@interface SickWriteController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (strong, nonatomic) IBOutlet UITextField *textName;
@property (strong, nonatomic) IBOutlet UITextField *textNumber;
@property (strong, nonatomic) IBOutlet UITextField *textIphone;
//病例种类
@property (strong, nonatomic) IBOutlet UILabel *labClassBing;
//病例细分
@property (strong, nonatomic) IBOutlet UILabel *fenLeiBing;
//病例照片
@property (strong, nonatomic) IBOutlet UIImageView *imageViewBing;
//手势对象View
@property (strong, nonatomic) IBOutlet UIView *topView;
@property (strong, nonatomic) IBOutlet UIView *bottomView;
//动画label
@property (strong, nonatomic) IBOutlet UILabel *oneLabel;
@property (strong, nonatomic) IBOutlet UILabel *twoLabel;
@property (strong, nonatomic) IBOutlet UILabel *threeLabel;
//
@property (strong, nonatomic) IBOutlet UIView *lastView;
//提交按钮
@property(nonatomic,weak)UIBarButtonItem *buttonBar;
//变化按钮记录
@property(nonatomic,assign)BOOL isAngin;
@property(nonatomic,weak)LabelGroupView *buttonGroupView;
//病例描述文字
@property(nonatomic,copy)NSString *titleMiaoShu;
//手势
@property(nonatomic,weak)UIGestureRecognizer *topTap;
@property(nonatomic,weak)UITapGestureRecognizer *tapBottomView;
@property(nonatomic,weak)UITapGestureRecognizer *tapImageViews;
//
@property(nonatomic,weak)UIImagePickerController *ipc;
@property(nonatomic,assign)NSInteger index;
//
@property (strong, nonatomic) IBOutlet UIButton *labBINGLI;
@end

@implementation SickWriteController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    [self setupUI];
    [self setupLabelArray];
    
}
-(void)setupUI
{   //创建BarButtonItem
    UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithTitle:@"提交" style:UIBarButtonItemStyleDone target:self action:@selector(clickBarButtonItemRight)];
    [button setTitleTextAttributes:@{NSFontAttributeName:MYFONT(20),NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = button;
    button.enabled = NO;
    _buttonBar = button;
    //创建手势
    UITapGestureRecognizer *tapTopView = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    _topTap = tapTopView;
    UITapGestureRecognizer *tapBottomView = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    _tapBottomView = tapBottomView;
    
    UITapGestureRecognizer *tapImageViews = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    _tapImageViews = tapImageViews;
    //添加
    [_topView addGestureRecognizer:tapTopView];
    [_bottomView addGestureRecognizer:tapBottomView];
    [_imageViewBing addGestureRecognizer:tapImageViews];
    _imageViewBing.userInteractionEnabled = YES;
    //输入栏的监听
    [_textName addTarget:self action:@selector(buttonIsEanble) forControlEvents:UIControlEventEditingChanged];
    [_textIphone addTarget:self action:@selector(buttonIsEanble) forControlEvents:UIControlEventEditingChanged];
    [_textNumber addTarget:self action:@selector(buttonIsEanble) forControlEvents:UIControlEventEditingChanged];
    //关闭交互
    _bottomView.userInteractionEnabled = NO;
    //Kvo监听键盘
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tongzhi) name:UIKeyboardWillChangeFrameNotification object:nil];
}
//手势的监听
-(void)tap:(UITapGestureRecognizer *)sender
{
    __weak SickWriteController *VC = self;
    if (sender.view == _topView) {
        //创建掩藏视图
        [self setupTopViewButton];
        
    }else if (sender.view == _bottomView)
    {
        //创建控制器
        ChooseTableController *VCChoose = [[ChooseTableController alloc]init];
        if ([self.labClassBing.text isEqualToString:@"肿瘤"]) {
            self.index = 1;
        }else
        if ([self.labClassBing.text isEqualToString:@"血液科"]) {
            self.index = 2;
        }else
        if ([self.labClassBing.text isEqualToString:@"神经"]) {
            self.index = 3;
        }else
        if ([self.labClassBing.text isEqualToString:@"心血管"]) {
            self.index = 4;
        }else
        if ([self.labClassBing.text isEqualToString:@"骨科"]) {
            self.index = 5;
        }else{
            self.index = 5;
        }
       // NSLog(@"%@",self.labClassBing.text);
        [VCChoose LoadDataWith:self.index Page:15];
        VCChoose.cellTextbloak = ^(NSString *str,NSInteger index){
            VC.fenLeiBing.text = str;
        };
        [self.navigationController pushViewController:VCChoose animated:YES];
        
    }else if (sender.view == _imageViewBing)
    {
        [[SickPushImageView sheradImgaviews] setupUIWithImageView:_imageViewBing];
        //从新添加手势 ---进入相册
        UITapGestureRecognizer *imageViewTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(openImageViewTouch)];
        [_imageViewBing addGestureRecognizer:imageViewTap];
        
        //Block回调
        [SickPushImageView sheradImgaviews].buttonBlock = ^(NSString *title){
            //病例描述文字
            VC.titleMiaoShu = title;
            if (title.length > 2) {
                VC.labBINGLI.titleLabel.text = @"病例填写:已填";
            }
            
            [VC.lastView addSubview:VC.imageViewBing];
            //手势
            [_imageViewBing removeGestureRecognizer:imageViewTap];
            [_imageViewBing addGestureRecognizer:VC.tapImageViews];
            
            [VC.imageViewBing mas_makeConstraints:^(MASConstraintMaker *make)
             {
                //回
                make.left.equalTo(VC.lastView.subviews.firstObject);
                make.top.equalTo(VC.lastView.subviews.firstObject.mas_bottom).offset(8);
                make.height.equalTo(@110);
                make.width.equalTo(@70);
            }];
            
        };
    }
}
//病例类型创建控件
-(void)setupTopViewButton
{
    _isAngin = !_isAngin;
    if (_isAngin) {
         [UIView animateWithDuration:0.5 animations:^{
             
            _buttonGroupView.alpha = 1;
            CGRect frame = self.topView.frame;
            frame.size.height = 132;
            self.topView.frame = frame;
            _bottomView.transform = CGAffineTransformMakeTranslation(0, 132-38);
            _lastView.transform = CGAffineTransformMakeTranslation(0, 132-38);
        }];
    }else{
        [UIView animateWithDuration:0.5 animations:^{
            _buttonGroupView.alpha = 0;
            
            CGRect frame = self.topView.frame;
            frame.size.height = 38;
            self.topView.frame = frame;
            _bottomView.transform = CGAffineTransformIdentity;
            _lastView.transform = CGAffineTransformIdentity;
        }];
    }
}
//创建label
-(void)setupLabelArray
{
    LabelGroupView *labelView = [[LabelGroupView alloc]init];
    _buttonGroupView = labelView;
    [_topView addSubview: labelView];
    
    _buttonGroupView.backgroundColor = [UIColor colorWithWhite:0.92 alpha:1];
    labelView.alpha = 0;
    
    [_buttonGroupView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_topView);
        make.top.equalTo(_topView.subviews.firstObject.mas_bottom).offset(15);
        make.bottom.equalTo(_buttonGroupView.subviews.lastObject).offset(10);
    }];
    _buttonGroupView.grunpButton = ^(NSString *title){
        _isAngin = 1;
        _bottomView.userInteractionEnabled = YES;
        _labClassBing.text = title;
        [self setupTopViewButton];
    };
}

//提交按钮监听
-(void)clickBarButtonItemRight
{
    //数组初始化
    PresonMondel *dict = [[PresonMondel alloc]init];
    dict.name = self.textName.text;
    dict.number = self.textNumber.text;
    dict.IPhone = self.textIphone.text;
    dict.ClassBING = self.labClassBing.text;
    dict.FenLeiBING = self.fenLeiBing.text;
    dict.casesPhoto = self.imageViewBing.image;
    dict.title = self.titleMiaoShu;
    dict.times = [self timeWithNew];
    
    if (self.navBarIitmBlock)
    {
        self.navBarIitmBlock(dict);
    }
    [self.navigationController popViewControllerAnimated:YES];
}
//当前的时间
-(NSString *)timeWithNew
{
    //把数字从1970年转变到nadate对象
    NSDate *currentData = [NSDate date];
    //NSCalendar *cal = [NSCalendar currentCalendar];
    //时间的格式化
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateFormat = @"MM:dd HH:mm";
    return [df stringFromDate:currentData];
}

//IsEanble
-(void)buttonIsEanble
{
    if (_textNumber.text.length > 0 && _textIphone.text.length && _textName.text.length) {
        _buttonBar.enabled = YES;
    }
}
//打开相册
-(void)openImageViewTouch
{
    [UIApplication sharedApplication].keyWindow.subviews[1].alpha = 0;
    [UIApplication sharedApplication].keyWindow.subviews[2].alpha = 0;
    [UIApplication sharedApplication].keyWindow.subviews[3].alpha = 0;
    [UIApplication sharedApplication].keyWindow.subviews[4].alpha = 0;
    // 1.判断相册是否可以打开
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return;
    // 2. 创建图片选择控制器
    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
    _ipc = ipc;
    // 3. 设置打开照片相册类型(显示所有相簿)
    ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    // 4.设置代理
    ipc.delegate = self;
    
    // 5.modal出这个控制器
    [self addChildViewController:ipc];
    [self.view addSubview:ipc.view];
    [[UIApplication sharedApplication].keyWindow addSubview:ipc.view];
    //[self presentViewController:ipc animated:YES completion:nil];
    
}
//该代理方法仅适用于只选取图片时
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    self.imageViewBing.image = image;
    //
    [_ipc.view removeFromSuperview];
    [_ipc removeFromParentViewController];
    //
    [UIApplication sharedApplication].keyWindow.subviews[1].alpha = 1;
    [UIApplication sharedApplication].keyWindow.subviews[2].alpha = 1;
    [UIApplication sharedApplication].keyWindow.subviews[3].alpha = 1;
    [UIApplication sharedApplication].keyWindow.subviews[4].alpha = 1;
}

//键盘的监听
-(void)tongzhi{
    self.topView.userInteractionEnabled = NO;
    self.bottomView.userInteractionEnabled = NO;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(ViewKeyBoards)];
    [self.view addGestureRecognizer:tap];
}
//删除通知
-(void)ViewKeyBoards
{
    [self.view endEditing:YES];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    self.topView.userInteractionEnabled = YES;
    self.bottomView.userInteractionEnabled = YES;
}
-(void)dealloc
{
    
}
@end
